<?php
namespace app\admin\controller;
use app\admin\controller\BaseAdmin;
use app\index\model\Shareimg;
use function Couchbase\defaultDecoder;
use GatewayWorker\Lib\Gateway;
use think\Db;
use think\Request;
use app\admin\model\Company as CompanyModel;
use app\admin\model\Position as PositionModel;
class Position extends BaseAdmin
{

    /**
     * 列表
     * @return mixed
     */
    public function index(Request $request){
        if (request()->isAjax()){
            return PositionModel::getlist($request);
        }else{
            $company = '';
            if ($company_id = $request->get('company_id')) {
                $company = \app\admin\model\Company::find($company_id)->toArray();

            }
            $this->assign('company', $company);
            $pageno = input('param.pageno', 1);
            $this->assign('pageno', $pageno);
            return $this->fetch();
        }
    }

    /**
     **详情页
     **/
    public function info(Request $request){
        // 接口请求
        if(request()->isAjax()) {
            $position = PositionModel::find($request->get('id'));
            // if($position){
            //     $province_id = 0;
            //     $pid = db('city')->where('id',$position['region_id'])->value('pid');
            //     if($pid  != 0){
            //         $pid = db('city')->where('id',$pid)->value('pid');
            //     }
            //     $position['province_id'] = $pid;
            // }
            $company  = db('company')->field('id,company_name')->select();
            $province = db('city')->field('id,name')->where('pid',0)->select();
            $jobtypedata  = db('jobtype')->select();
            $position['company'] = $company;
            $position['province'] = $province;
            $position['jobtypedata'] = $jobtypedata;
            return returnData($position);
        }

        $pageno = request()->get('pageno')?request()->get('pageno'):1;
        $this->assign('id',request()->get('id'));
        $this->assign('pageno', $pageno);
        return $this->fetch();
    }


    /**
    **修改添加
    **/
    public function set(){
        if(request()->isAjax()) {
            if(request()->isPost()){
                $post = request()->post();
                unset($post['province_id']);
                $id = !empty($post['id'])?$post['id']:0;
                if(!empty($post['company'])) unset($post['company']);
                if(!empty($post['province'])) unset($post['province']);
                if(!empty($post['jobtypedata'])) unset($post['jobtypedata']);
                if(!empty($post['company_id'])){
                    $company = db('company')->where('id', $post['company_id'])->field('id,company_name,company_mobile,logo')->find();
                    $post['company_name']   = $company['company_name'];
                    $post['company_mobile'] = $company['company_mobile'];
                    $post['company_logo']   = $company['logo'];
                }

                if($id){
                    unset($post['id']);
                    $res = Db::name('position')->where('id', $id)->update($post);
                    if($res!==false){
                        return ['status'=>true, 'message'=>'更新成功'];
                    }else{
                        return ['status'=>false,'message'=>'更新失败'];
                    }
                }else{
                    $post['ctime'] = time();
                    $res = Db::name('position')->insertGetId($post);
                    if($res){
                        return ['status'=>true, 'message'=>'新增成功'];
                    }else{
                        return ['status'=>false,'message'=>'新增失败'];
                    }
                }
            }else{
                $company  = db('company')->field('id,company_name')->select();
                $province = db('city')->field('id,name')->where('pid',0)->select();
                $welfaredata  = db('welfare')->select();
                $jobtypedata  = db('jobtype')->select();
                $userdata = db('user')->field('id,nickName name')->where('nickName','<>','')->select();
                $position['company']  = $company;
                $position['province'] = $province;
                $position['welfaredata']  = $welfaredata;
                $position['userdata'] = $userdata;
                $position['jobtypedata'] = $jobtypedata;
                return returnData($position);
            }
        }else{
            return $this->fetch();
        }

    }



    public function city()
    {
        if(request()->isPost()){
            $post = request()->post();
            $id = $post['pid'];
            $province = db('city')->field('id,name')->where('pid',$id)->select();
            return $province;
        }
    }



    /**
     * 删除
     * @return array
     */
    public function remove(){
        $ids = request()->post('ids');
        if(request()->isAjax()){
            $ids = json_decode($ids, true);
            $result = (new PositionModel())->whereIn('id', $ids)->delete();
            if($result){
                return returnData();
            }else{
                return ['status'=>false,'message'=>'删除失败!'];
            }
        }else{
            new \HttpException(502,'请求不合法');
        }
    }



    public function config(){
        if (request()->isAjax()){
            if(request()->isPost()){
                $post = request()->post();
                $imgArr = [];
                if(!empty($post['ad'])){
                    $post['ad'] = $post['ad']?join(',',$post['ad']):'';
                }
                $config = Db::name('config')->find();
                $post['basic_price'] && $post['basic_price'] = $post['basic_price']*100;
                $post['repair_10'] && $post['repair_10'] = $post['repair_10']*100;
                $post['repair_24'] && $post['repair_24'] = $post['repair_24']*100;

                if($config){
                    $res = db('config')->where('id', $config['id'])->update($post);
                }else{
                    $res = db('config')->insertGetId($post);
                }

                if($res !== false){
                    return ['status'=>true,'message'=>'编辑成功'];
                }else{
                    return ['status'=>false,'message'=>'编辑成功'];
                }
            }else{
                $config = Db::name('config')->find();
                if($config){
                    $config['basic_price'] = $config['basic_price']/100;
                    $config['repair_10'] = $config['repair_10']/100;
                    $config['repair_24'] = $config['repair_24']/100;
                    $ad = [];
                    if(!empty($config['ad'])){
                        $ad = explode(',', $config['ad']);
                    }
                    $config['ad'] = $ad;
                }
                return $config;
            }
        }else{
            return $this->fetch();
        }
    }

    public function getonline()
    {
        $data = Gateway::getAllClientIdList();
        dump($data);
    }

    # 审核通过
    public function pass(Request $request)
    {
        $company = \app\admin\model\Position::find($request->post('id'));
        $company->status = 1; // 0 未审核 1 审核；
        $company->save();
        return returnData();
    }


    # 修改信息
    public function updateinfo(Request $request)
    {
        $id = $request->post('id');
        $data = $request->post('data');
        $positonModel = PositionModel::find($id);
        $positonStatus= $positonModel->save($data);
        return returnData($positonModel);
    }


}
